package com.example.springboot.controller;

import com.example.springboot.domain.Product;
import com.example.springboot.domain.Result;
import com.example.springboot.service.ProductService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Tag(name = "数据库的增删改查", description = "Mybatis-Flex")
public class ProductController {
    //添加信息
    @Autowired
    private ProductService productService;

    // 增加一个产品
    @Operation(summary = "body请求", description = "增加一个产品")
    @PostMapping("/add")
    public Result add(@RequestBody Product product) {
        boolean save = productService.save(product);
        return Result.success(save);
    }

    //    根据id查询
    @GetMapping("/{id}")
    @Operation(summary = "通过id查询产品信息", description = "根据id查询")
    public Result get(@PathVariable Long id) {
        Product product = productService.getById(id);
        return Result.success(product);
    }

    //修改库存
    @Operation(summary = "修改数据库指定信息", description = "更新库存数据")
    @PutMapping("/update")
    // update
    public Result update(@PathVariable Integer stock,@PathVariable Integer id) {
        Product product = new Product();
        product.setStock(stock);
        product.setId(id);
        boolean b = productService.updateById(product);
        return Result.success(b);
    }
    @GetMapping ("/like")
    public Result like(@PathVariable String likeName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",likeName);
        List<Product> list = productService.list(queryWrapper);
        return Result.success(list);
    }

}
